package com.tfb.service.impl;

import com.tfb.rong.Assessment;
import com.tfb.rong.AssessmentBean;
import com.tfb.rong.AssessmentCommentBean;
import com.tfb.rong.AssessmentComments;
import com.tfb.rong.AssessmentWithBLOBs;
import com.tfb.rong.COVERPAGE;
import com.tfb.rong.Device;
import com.tfb.rong.MedicationPage;
import com.tfb.rong.Medication_HomeRemedyRecord;
import com.tfb.rong.Medication_HomeRemedyRecord_item;
import com.tfb.rong.Medication_MedicationList;
import com.tfb.rong.Medication_MedicationList_Data;
import com.tfb.rong.Medication_Risk;
import com.tfb.rong.Medication_Risk_DrugReaction;
import com.tfb.rong.Medication_Risk_DrugReaction_PotentialHazard;
import com.tfb.rong.PersonalInformationPage;
import com.tfb.rong.PersonalInformation_OtherContacts;
import com.tfb.rong.PersonalInformation_Professionals;
import com.tfb.rong.Users;
import com.tfb.rong.cover_OtherPresentPeople;
import com.tfb.service.UserAssessmentI;
import com.tfb.service.utility.MD5Util;
import com.tfb.web.controller.Credentials;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.tfb.dao.*;
import com.tfb.rong.BodyMap;
import com.tfb.rong.ImageInfo;
/* service id is userService  */

@Service("userAssessmentService")
public class UserAssessmentService implements UserAssessmentI {

    @Autowired
    private AssessmentMapper assessMapper;
    @Autowired
    private cover_OtherPresentPeopleMapper coverMapper;
    @Autowired
    private PersonalInformation_OtherContactsMapper poMapper;
    @Autowired
    private PersonalInformation_ProfessionalsMapper ppMapper;
    @Autowired
    private Medication_MedicationList_DataMapper mdMapper;
    @Autowired
    private Medication_Risk_DrugReaction_PotentialHazardMapper mrdpMapper;
    @Autowired
    private UsersMapper userMapper;
    @Autowired
    private Medication_HomeRemedyRecord_itemMapper mhiMapper;
    @Autowired
    private AssessmentCommentsMapper assessCommentMapper;
    @Autowired
    private LocationDataMapper locationMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private BodyMapMapper bodyMapMapper;
    @Autowired
    private ImageInfoMapper imageInfoMapper;

    @Override
    public ImageInfo selectByBodyMapID(String bodymapid) {
        return imageInfoMapper.selectByBodyMapID(bodymapid);
    }

    @Override
    public int insertBodyMapImageInfo(ImageInfo imageInfo) {
        return imageInfoMapper.insertBodyMapImageInfo(imageInfo);
    }

    @Override
    public int insertBodyMap(BodyMap bodyMap) {
        return bodyMapMapper.insertBodyMap(bodyMap);
    }

    @Override
    public BodyMap selectByAssessID(String assessid) {
        return bodyMapMapper.selectByAssessID(assessid);
    }

    @Override
    public int deleteOldLocationData() {
        return locationMapper.deleteOldData();
    }

    @Override
    public int addAssessmentComments(AssessmentCommentBean acb) {
        // delete
        AssessmentComments acomment = new AssessmentComments();
        acomment.setAssessmentId(acb.getAssessmentBean().getId());
        acomment.setStatus(0);
        assessCommentMapper.updateStatus(acomment);
        List<AssessmentComments> aclist = acb.getAllComments();
        for (AssessmentComments ac : aclist) {
            if (ac != null) {
                ac.setStatus(1);
                assessCommentMapper.insert(ac);
            }
        }
        return updateAssessment(acb.getAssessmentBean());
    }

    @Override
    public int AddAssessment(AssessmentBean ab) {
        AssessmentWithBLOBs awb = ab.toDatabaseBean();
        int ret = assessMapper.insert(awb);
        // if (ret == 0) {
        if (ab.getCover() != null && ab.getCover().getOtherPresentPeople() != null) {
            for (int i = 0; i < ab.getCover().getOtherPresentPeople().size(); i++) {
                cover_OtherPresentPeople cop = ab.getCover().getOtherPresentPeople().get(i);
                if (cop != null) {
                    cop.setAssessmentId(ab.getId());
                    ret = coverMapper.insert(cop);
                    System.out.println("insert into assessment return :" + ret);
                }
            }
        }
        // }
        if (null != ab.getPersonalInformation() && ab.getPersonalInformation().getOtherContacts() != null) {
            for (int i = 0; i < ab.getPersonalInformation().getOtherContacts().size(); i++) {
                PersonalInformation_OtherContacts po = ab.getPersonalInformation().getOtherContacts().get(i);
                if (po != null) {
                    po.setAssessmentId(ab.getId());
                    ret = poMapper.insert(po);
                }
            }
        }
        if (null != ab.getPersonalInformation() && ab.getPersonalInformation().getProfessionals() != null) {
            for (int i = 0; i < ab.getPersonalInformation().getOtherContacts().size(); i++) {
                PersonalInformation_Professionals pp = ab.getPersonalInformation().getProfessionals().get(i);
                if (pp != null) {
                    pp.setAssessmentId(ab.getId());
                    ret = ppMapper.insert(pp);
                }
            }
        }

        if (null != ab.getMedication() && ab.getMedication().getMedicationList() != null
            && ab.getMedication().getMedicationList().getLstdata() != null) {
            for (int i = 0; i < ab.getMedication().getMedicationList().getLstdata().size(); i++) {
                Medication_MedicationList_Data mmd = ab.getMedication().getMedicationList().getLstdata().get(i);
                if (mmd != null) {
                    mmd.setAssessmentId(ab.getId());
                    ret = mdMapper.insert(mmd);
                }
            }
        }

        if (null != ab.getMedication() && ab.getMedication().getRisk() != null
            && ab.getMedication().getRisk().getDrugReaction() != null
            && ab.getMedication().getRisk().getDrugReaction().getPotentialHazard() != null) {
            for (int i = 0; i < ab.getMedication().getRisk().getDrugReaction().getPotentialHazard().size(); i++) {
                Medication_Risk_DrugReaction_PotentialHazard mrdp = ab.getMedication().getRisk().getDrugReaction()
                .getPotentialHazard().get(i);
                if (mrdp != null) {
                    mrdp.setAssessmentId(ab.getId());
                    ret = mrdpMapper.insert(mrdp);
                }
            }
        }
        if (null != ab.getMedication() && ab.getMedication().getRisk() != null
            && ab.getMedication().getHomeRemedyRecord() != null
            && ab.getMedication().getHomeRemedyRecord().getAllergies_data() != null) {
            for (int i = 0; i < ab.getMedication().getHomeRemedyRecord().getAllergies_data().size(); i++) {
                Medication_HomeRemedyRecord_item mhi = ab.getMedication().getHomeRemedyRecord().getAllergies_data()
                .get(i);
                if (mhi != null) {
                    mhi.setAssessmentId(ab.getId());
                    ret = mhiMapper.insert(mhi);
                }
            }
        }
        return ret;
    }

    @Override
    public int updateAssessment(AssessmentBean ab) {
        AssessmentWithBLOBs dbwithblob = ab.toDatabaseBean();
        assessMapper.updateByPrimaryKeyWithBLOBs(dbwithblob);
        // also need to update the others
        int ret = 0;
        // first delete all
        coverMapper.deleteByAssessMent(ab.getId());

        if (ab.getCover() != null && ab.getCover().getOtherPresentPeople() != null) {
            for (int i = 0; i < ab.getCover().getOtherPresentPeople().size(); i++) {
                cover_OtherPresentPeople cop = ab.getCover().getOtherPresentPeople().get(i);
                if (cop != null) {
                    cop.setAssessmentId(ab.getId());
                    ret = coverMapper.insert(cop);
                    System.out.println("insert into assessment return :" + ret);
                }
            }
        }
        // first delete all
        poMapper.deleteByAssessMent(ab.getId());

        if (null != ab.getPersonalInformation() && ab.getPersonalInformation().getOtherContacts() != null) {
            for (int i = 0; i < ab.getPersonalInformation().getOtherContacts().size(); i++) {
                PersonalInformation_OtherContacts po = ab.getPersonalInformation().getOtherContacts().get(i);
                if (po != null) {
                    po.setAssessmentId(ab.getId());
                    ret = poMapper.insert(po);
                }
            }
        }
        // delte first
        ppMapper.deleteByAssessMent(ab.getId());
        // insert the new if have
        if (null != ab.getPersonalInformation() && ab.getPersonalInformation().getProfessionals() != null) {
            for (int i = 0; i < ab.getPersonalInformation().getProfessionals().size(); i++) {
                PersonalInformation_Professionals pp = ab.getPersonalInformation().getProfessionals().get(i);
                if (pp != null) {
                    pp.setAssessmentId(ab.getId());
                    ret = ppMapper.insert(pp);
                }
            }
        }
        mdMapper.deleteByAssessMent(ab.getId());
        if (null != ab.getMedication() && ab.getMedication().getMedicationList() != null
            && ab.getMedication().getMedicationList().getLstdata() != null) {
            for (int i = 0; i < ab.getMedication().getMedicationList().getLstdata().size(); i++) {
                Medication_MedicationList_Data mmd = ab.getMedication().getMedicationList().getLstdata().get(i);
                if (mmd != null) {
                    mmd.setAssessmentId(ab.getId());
                    ret = mdMapper.insert(mmd);
                }
            }
        }
        mrdpMapper.deleteByAssessMent(ab.getId());
        if (null != ab.getMedication() && ab.getMedication().getRisk() != null
            && ab.getMedication().getRisk().getDrugReaction() != null
            && ab.getMedication().getRisk().getDrugReaction().getPotentialHazard() != null) {
            for (int i = 0; i < ab.getMedication().getRisk().getDrugReaction().getPotentialHazard().size(); i++) {
                Medication_Risk_DrugReaction_PotentialHazard mrdp = ab.getMedication().getRisk().getDrugReaction()
                .getPotentialHazard().get(i);
                if (mrdp != null) {
                    mrdp.setAssessmentId(ab.getId());
                    ret = mrdpMapper.insert(mrdp);
                }
            }
        }
        mhiMapper.deleteByAssessMent(ab.getId());
        if (null != ab.getMedication() && ab.getMedication().getRisk() != null
            && ab.getMedication().getHomeRemedyRecord() != null
            && ab.getMedication().getHomeRemedyRecord().getAllergies_data() != null) {
            for (int i = 0; i < ab.getMedication().getHomeRemedyRecord().getAllergies_data().size(); i++) {
                Medication_HomeRemedyRecord_item mhi = ab.getMedication().getHomeRemedyRecord().getAllergies_data()
                .get(i);
                if (mhi != null) {
                    mhi.setAssessmentId(ab.getId());
                    ret = mhiMapper.insert(mhi);
                }
            }
        }
        return 0;
    }

    @Override
    public int deleteAssessment(Assessment ab) {
        assessMapper.deleteByPrimaryKey(ab.getId());
        // remove other data
        coverMapper.deleteByAssessMent(ab.getId());
        poMapper.deleteByAssessMent(ab.getId());
        ppMapper.deleteByAssessMent(ab.getId());
        ;
        mdMapper.deleteByAssessMent(ab.getId());
        ;
        mrdpMapper.deleteByAssessMent(ab.getId());
        ;
        assessCommentMapper.deleteByAssessMent(ab.getId());
        ;
        return 0;
    }

    @Override
    public int getNextID(String strtype) {
        return assessMapper.getNextID(strtype);
    }

    @Override
    public List<Users> selectAllUserInfoByStatusAndType(Users record) {
        return userMapper.selectAllUserInfoByStatusAndType(record);
    }

    @Override
    public Users selectUserByPrimaryKey(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public Users selectUserInfo(String openID) {
        return userMapper.selectUserInfoByOpenID(openID);
    }

    @Override
    public Users selectUserInfo(Credentials credential) {
        Users user = new Users();
        user.setPassword(MD5Util.MD5(credential.getPassword()));
        if (null == credential.getUsername())
            return null;
        user.setName(credential.getUsername());
        user.setNickname(credential.getUsername());
        // user.setKeyword(straction);
        if (credential.getUsername().contains("@")) {
            user.setEmail(credential.getUsername());

        } else if (credential.getUsername().length() == 11 && MD5Util.isNumeric(credential.getUsername())) {
            user.setTel(credential.getUsername());
            user.setQq(credential.getUsername());

        }
        System.out.println("input password is:" + user.getPassword());
        System.out.println("input user is:" + user.getName());
        return userMapper.selectUserInfo(user);
    }

    @Override
    public String getAuthorizationKeyword(Credentials credential) {
        Users user = new Users();
        user.setPassword(MD5Util.MD5(credential.getPassword()));
        if (null == credential.getUsername())
            return null;
        user.setName(credential.getUsername());
        user.setNickname(credential.getUsername());
        // user.setKeyword(straction);
        if (credential.getUsername().contains("@")) {
            user.setEmail(credential.getUsername());

        } else if (credential.getUsername().length() == 11 && MD5Util.isNumeric(credential.getUsername())) {
            user.setTel(credential.getUsername());
            user.setQq(credential.getUsername());

        }
        System.out.println("input password is:" + user.getPassword());
        System.out.println("input user is:" + user.getName());
        String strkeyword = userMapper.getUserKeyword(user);

        return strkeyword;
    }

    @Override
    public List<cover_OtherPresentPeople> queryAllOtherPresentPeopleByAssessment(String strid) {
        return coverMapper.selectAllByAssessMent(strid);
    }

    @Override
    public AssessmentBean queryAssessmentwithoucomments(String strid) {
        AssessmentWithBLOBs abblob = assessMapper.selectByPrimaryKey(strid);
        AssessmentBean ab = new AssessmentBean(abblob);

        List<cover_OtherPresentPeople> ls_otherpeople = coverMapper.selectAllByAssessMent(strid);
        if (ab.getCover() == null && ls_otherpeople != null && ls_otherpeople.size() > 0) {
            ab.setCover(new COVERPAGE());
        }
        if (ls_otherpeople.size() > 0)
            ab.getCover().setOtherPresentPeople(ls_otherpeople);

        List<Medication_MedicationList_Data> ls_mmd = mdMapper.selectAllByAssessMent(strid);
        if (ab.getMedication() == null) {
            ab.setMedication(new MedicationPage());
        }
        List<Medication_Risk_DrugReaction_PotentialHazard> ls_mrdp = this.mrdpMapper.selectAllByAssessMent(strid);

        if (ls_mrdp != null && ls_mrdp.size() > 0 && ab.getMedication().getRisk() == null) {
            ab.getMedication().setRisk(new Medication_Risk());
        }
        if (ls_mrdp != null && ls_mrdp.size() > 0 && ab.getMedication().getRisk().getDrugReaction() == null) {
            ab.getMedication().getRisk().setDrugReaction(new Medication_Risk_DrugReaction());
        }
        if (ls_mrdp != null && ls_mrdp.size() > 0) {
            ab.getMedication().getRisk().getDrugReaction().setPotentialHazard(ls_mrdp);
        }
        if (ls_mmd != null && ls_mmd.size() > 0 && ab.getMedication().getMedicationList() == null) {
            ab.getMedication().setMedicationList(new Medication_MedicationList());
        }
        if (ls_mmd != null && ls_mmd.size() > 0) {
            ab.getMedication().getMedicationList().setLstdata(ls_mmd);
        }
        List<Medication_HomeRemedyRecord_item> ls_mhi = mhiMapper.selectAllByAssessMent(strid);
        if (ls_mhi != null && ls_mhi.size() > 0 && ab.getMedication().getHomeRemedyRecord() == null) {
            ab.getMedication().setHomeRemedyRecord(new Medication_HomeRemedyRecord());
        }

        if (ls_mhi != null && ls_mhi.size() > 0) {
            ab.getMedication().getHomeRemedyRecord().setAllergies_data(ls_mhi);
        }

        List<PersonalInformation_OtherContacts> ls_po = this.poMapper.selectAllByAssessMent(strid);
        List<PersonalInformation_Professionals> ls_pp = this.ppMapper.selectAllByAssessMent(strid);
        if (((ls_po != null && ls_po.size() > 0) || (ls_pp != null && ls_pp.size() > 0))
            && ab.getPersonalInformation() == null) {
            ab.setPersonalInformation(new PersonalInformationPage());
        }
        if (ls_po != null && ls_po.size() > 0) {
            ab.getPersonalInformation().setOtherContacts(ls_po);
        }
        if (ls_pp != null && ls_pp.size() > 0) {
            ab.getPersonalInformation().setProfessionals(ls_pp);
        }
        return ab;
    }

    @Override
    public AssessmentCommentBean queryAssessMentDetail(String strid) {
        AssessmentCommentBean acbean = new AssessmentCommentBean();
        AssessmentWithBLOBs abblob = assessMapper.selectByPrimaryKey(strid);
        AssessmentBean ab = new AssessmentBean(abblob);
        acbean.setAssessmentBean(ab);
        List<cover_OtherPresentPeople> ls_otherpeople = coverMapper.selectAllByAssessMent(strid);
        if (acbean.getAssessmentBean() != null && acbean.getAssessmentBean().getCover() == null
            && ls_otherpeople != null && ls_otherpeople.size() > 0) {
            acbean.getAssessmentBean().setCover(new COVERPAGE());
        }
        if (ls_otherpeople.size() > 0)
            acbean.getAssessmentBean().getCover().setOtherPresentPeople(ls_otherpeople);

        List<Medication_MedicationList_Data> ls_mmd = mdMapper.selectAllByAssessMent(strid);
        if (acbean.getAssessmentBean() != null && acbean.getAssessmentBean().getMedication() == null) {
            acbean.getAssessmentBean().setMedication(new MedicationPage());
        }
        List<Medication_Risk_DrugReaction_PotentialHazard> ls_mrdp = this.mrdpMapper.selectAllByAssessMent(strid);
        if (ls_mrdp != null && ls_mrdp.size() > 0 && acbean.getAssessmentBean().getMedication().getRisk() == null) {
            acbean.getAssessmentBean().getMedication().setRisk(new Medication_Risk());
        }
        if (ls_mrdp != null && ls_mrdp.size() > 0
            && acbean.getAssessmentBean().getMedication().getRisk().getDrugReaction() == null) {
            acbean.getAssessmentBean().getMedication().getRisk().setDrugReaction(new Medication_Risk_DrugReaction());
        }
        if (ls_mrdp != null && ls_mrdp.size() > 0) {
            acbean.getAssessmentBean().getMedication().getRisk().getDrugReaction().setPotentialHazard(ls_mrdp);
        }
        if (ls_mmd != null && ls_mmd.size() > 0
            && acbean.getAssessmentBean().getMedication().getMedicationList() == null) {
            acbean.getAssessmentBean().getMedication().setMedicationList(new Medication_MedicationList());
        }
        if (ls_mmd != null && ls_mmd.size() > 0) {
            acbean.getAssessmentBean().getMedication().getMedicationList().setLstdata(ls_mmd);
        }

        List<Medication_HomeRemedyRecord_item> ls_mhi = mhiMapper.selectAllByAssessMent(strid);
        if (ls_mhi != null && ls_mhi.size() > 0
            && acbean.getAssessmentBean().getMedication().getHomeRemedyRecord() == null) {
            acbean.getAssessmentBean().getMedication().setHomeRemedyRecord(new Medication_HomeRemedyRecord());
        }

        if (ls_mhi != null && ls_mhi.size() > 0) {
            acbean.getAssessmentBean().getMedication().getHomeRemedyRecord().setAllergies_data(ls_mhi);
        }

        List<PersonalInformation_OtherContacts> ls_po = this.poMapper.selectAllByAssessMent(strid);
        List<PersonalInformation_Professionals> ls_pp = this.ppMapper.selectAllByAssessMent(strid);
        if (((ls_po != null && ls_po.size() > 0) || (ls_pp != null && ls_pp.size() > 0))
            && acbean.getAssessmentBean().getPersonalInformation() == null) {
            acbean.getAssessmentBean().setPersonalInformation(new PersonalInformationPage());
        }
        if (ls_po != null && ls_po.size() > 0) {
            acbean.getAssessmentBean().getPersonalInformation().setOtherContacts(ls_po);
        }
        if (ls_pp != null && ls_pp.size() > 0) {
            acbean.getAssessmentBean().getPersonalInformation().setProfessionals(ls_pp);
        }
        // it for coments
        List<AssessmentComments> ls_ac = assessCommentMapper.selectAllByAssessMent(strid);
        acbean.setAllComments(ls_ac);
        return acbean;
    }

    @Override
    public int updateUser(Users user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public List<Assessment> selectMyAssessment(Assessment record) {
        return assessMapper.selectMyAssessment(record);
    }

    @Override
    public int insertUserInfo(Users user) {
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        // default customer
        if (user.getType() == null) {
            user.setType(1);
        }
        user.setKeyword(",listassessment,getassessment,");
        user.setPassword(MD5Util.MD5(user.getPassword()));
        return userMapper.insert(user);
    }

    @Override
    public int addDevice(Device device) {
        return deviceMapper.insert(device);
    }

    @Override
    public int modifyDevice(Device device) {
        return deviceMapper.updateByPrimaryKey(device);
    }

    @Override
    public Users selectBindUserInfo(Users record) {
        return userMapper.selectBindUserInfo(record);
    }

    @Override
    public Users selectByLogin(String email, String password) {
        return userMapper.selectByLogin(email, MD5Util.MD5(password));
    }
}
